package de.hska.master.model;

import java.util.ArrayList;
import java.util.HashSet;

public class RandomModel extends Model {

	private int numberOfPoints;
	private int viewerWidth;
	private int viewerHeight;

	/**
	 * Generates a random-model instance
	 * 
	 * @param maxValue
	 *            height and width as x and y for randomly generated points
	 */
	public RandomModel(int maxValue) {
		super();
		numberOfPoints = maxValue;
		this.viewerHeight = maxValue * 100;
		this.viewerWidth = maxValue * 100;
		generateRandomPoints();
	}

	/**
	 * Generates a random-model instance
	 * 
	 * @param viewerHeight
	 *            max height (y-coordinate) of randomly generated points
	 * @param viewerWidth
	 *            max width (x-coordinate) of randomly generated points
	 */
	public RandomModel(int viewerHeight, int viewerWidth) {
		super();
		numberOfPoints = 20;
		this.viewerWidth = viewerWidth;
		this.viewerHeight = viewerHeight;
		generateRandomPoints();
	}

	private void generateRandomPoints() {
		HashSet<Point> set = new HashSet<Point>(numberOfPoints);
		for (int i = 0; i < numberOfPoints; ++i) {
			Point p = getRandomPoint();
			while (!set.add(p)) {// while set already contains this point
				p = getRandomPoint();
			}
		}

		points = new ArrayList<Point>(set);
		System.out.println();
		// For the time measurement a list would be better, because with a set
		// the java heap will run faster out of memory.
		// List<Point> generatedPoints = new ArrayList<Point>();
		// for (int i = 0; i < numberOfPoints; i++) {
		// Point p = getRandomPoint();
		// // if (!generatedPoints.contains(p))
		// generatedPoints.add(p);
		// }
		// points = new ArrayList<Point>(generatedPoints);
	}

	private Point getRandomPoint() {
		double x = 20 + (getRandomValue(viewerWidth) * 0.8);
		double y = 40 + (getRandomValue(viewerHeight) * 0.8);
		return new Point(x, y);
	}

	private double getRandomValue(int maximum) {
		return (Math.random() * maximum);
	}
}
